package com.example.library.service;

import com.example.library.bean.model.Book;
import com.example.library.bean.model.BookType;
import com.example.library.bean.model.BorrowOrder;
import com.example.library.bean.model.User;
import com.example.library.bean.response.FailureCause;
import com.example.library.bean.response.FailureResponse;
import com.example.library.bean.response.Response;
import com.example.library.bean.response.SuccessResponse;
import com.example.library.dao.BookRepository;
import com.example.library.dao.BookTypeRepository;
import com.example.library.dao.BorrowOrderRepository;
import com.example.library.dao.UserRepository;
import com.example.library.util.Constant;
import com.example.library.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@RestController
public class BorrowOrderService {

    @Autowired
    private BorrowOrderRepository borrowOrderRepository;

    @Autowired
    private BookRepository bookRepository;

    /**
     * 添加借阅
     * @param user
     * @param book
     * @return
     */
    public Response addBorrow(User user, Book book) {
        long count = borrowOrderRepository.countAllByBook_BookIdAndUser_UserIdAndStatus(book.getBookId(),user.getUserId(), Constant.NORMAL);
        if(count > 0) {
            return new FailureResponse(FailureCause.REPEAT_BORROW);
        }
        Book targetBook = bookRepository.findByBookId(book.getBookId());
        if(targetBook.getNumber() <= 0) {
            return new FailureResponse(FailureCause.NO_MORE_BOOK);
        }
        targetBook.setNumber(targetBook.getNumber()-1);
        bookRepository.save(targetBook);
        //order
        BorrowOrder order = new BorrowOrder();
        order.setBook(book);
        order.setUser(user);
        LocalDate today = LocalDate.now();
        LocalDate ddl = today.plusMonths(1);
        order.setBorrowDate(DateUtil.localDate2Date(today));
        order.setReturnDeadline(DateUtil.localDate2Date(ddl));
        borrowOrderRepository.save(order);
        return new SuccessResponse("借阅成功");
    }

    /**
     * 归还书本
     * @param userId
     * @param bookId
     * @return
     */
    public Response returnBorrow(long userId, long bookId) {
        BorrowOrder order = borrowOrderRepository.findByBook_BookIdAndUser_UserIdAndStatus(bookId,userId,Constant.NORMAL);
        if(order == null) {
            return new FailureResponse(FailureCause.REPEAT_BORROW);
        }
        Book targetBook = bookRepository.findByBookId(bookId);
        targetBook.setNumber(targetBook.getNumber()+1);
        bookRepository.save(targetBook);
        //order
        LocalDate today = LocalDate.now();
        order.setReturnDate(DateUtil.localDate2Date(today));
        order.setStatus(Constant.RETURNED);
        borrowOrderRepository.save(order);
        return new SuccessResponse("归还成功");
    }

    /**
     * 获取我的借阅
     * @param userId
     * @return
     */
    public Response getBorrowBooks(long userId,int status) {
        List<BorrowOrder> orders = borrowOrderRepository.findAllByUser_UserIdAndStatus(userId,status);
        Response response = new SuccessResponse();
        response.setResult(orders);
        return response;
    }


}
